/**
 * Captain
 * © 2018 Captain copyright，All rights reserved.
 * http://www.sccaptain.com.cn
 * 
 * JAVA : 8
 * 文  件  名: ClassifyController.java
 * 创  建  人: QinYoubin (qin.youbin@sccaptain.com.cn)
 * 创建时间: 2018年4月24日 上午9:45:20
 * 版         本: 1.0.0
 * 备         注:
 * 修订历史:
 */
package com.material.c2c.web.classify.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cpt.framework.web.JsonResult;
import com.material.c2c.classify.entity.SPFL;
import com.material.c2c.classify.model.ClassifyDTO;
import com.material.c2c.classify.model.ClassifyTreeNode;
import com.material.c2c.classify.service.ClassifyService;
import com.material.c2c.web.classify.model.ClassifyQueryVO;
import com.material.c2c.web.util.ControllerUtil;

/**
 * 分类管理控制层
 * @since 1.0
 * @version 1.0
 * @author QinYoubin (qin.youbin@sccaptain.com.cn)
 */
@RestController
@RequestMapping("/classify")
public class ClassifyController {
  @Resource
  private ClassifyService classifyService;
  /**
   * 新增
   * @param dto
   * @return
   * @since 1.0
   * @version 1.0
   * @author QinYoubin (qin.youbin@sccaptain.com.cn)
   */
  @PostMapping("/add")
  public JsonResult add(@RequestBody SPFL dto) {
    return ControllerUtil.getResult( classifyService.add(dto));
  }
  /**
   * 更新
   * @param dto
   * @return
   * @since 1.0
   * @version 1.0
   * @author QinYoubin (qin.youbin@sccaptain.com.cn)
   */
  @PostMapping("/update")
  public JsonResult update(@RequestBody ClassifyDTO dto) {
   return ControllerUtil.getResult( classifyService.update(dto));
  }
  /**
   * 删除
   * @param id
   * @return
   * @since 1.0
   * @version 1.0
   * @author QinYoubin (qin.youbin@sccaptain.com.cn)
   */
  @GetMapping("/delete/{id}")
  public JsonResult delete(@PathVariable String id) {
    return ControllerUtil.getResult( classifyService.delete(id));
  }
  @PostMapping("/search")
  public JsonResult search(@RequestBody ClassifyQueryVO query) {
    return JsonResult.success(classifyService.search(query.getPageable(), query.getPid(), query.getKey()));
  }
  @PostMapping("/page")
  public JsonResult page(@RequestBody ClassifyQueryVO query) {
    return JsonResult.success(classifyService.page(query.getPageable(), query.getKey()));
  }
  @GetMapping("/getClassifyTree")
  public JsonResult getClassifyTree() {
	  ClassifyTreeNode node = new ClassifyTreeNode();
	  node.setTitle("商品分类");
	  node.setExpand(true);
	  node.setChildren(classifyService.createTree());
	  List<ClassifyTreeNode> res = new ArrayList<ClassifyTreeNode>();
	  res.add(node);
    return JsonResult.success(res);
  }
  @GetMapping("/getOne/{id}")
  public JsonResult getOne(@PathVariable String id) {
    return JsonResult.success(classifyService.getOne(id));
  }
}
